package com.witte.controller;

import com.witte.entity.RespBean;

import com.witte.entity.UserRoot;
import com.witte.service.UserRootService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@ResponseBody
@RequestMapping(value="/api/ur")
@CrossOrigin
public class UserRootController {
    @Autowired
    private UserRootService userRootService;


    @RequestMapping(value = "/getAll")
    public RespBean getAll(){
        System.out.println(userRootService.selectAll());
        RespBean respBean =RespBean.ok(null, userRootService.selectAll());
        return respBean ;
    }

    @RequestMapping(value = "/selectByRule")
    public RespBean selectRule(@RequestBody  UserRoot userRoot){
        System.out.println(userRootService.selectByRule(userRoot));
        RespBean respBean =RespBean.ok(null,userRootService.selectByRule(userRoot));
        return  respBean;
    }

    @RequestMapping(value = "/deleteUser")
    public RespBean deleteUser(@RequestBody UserRoot userRoot){
        return RespBean.ok(null,userRootService.deleteByPrimaryKey(userRoot.getId()));
    }

    @RequestMapping(value = "/updateUser")
    public RespBean updateUser(@RequestBody UserRoot userRoot){
        RespBean respBean =RespBean.ok(null,userRootService.updateByPrimaryKey(userRoot));
        return respBean;
    }

    @RequestMapping(value = "/insertUser")
    public  RespBean insertUser(@RequestBody UserRoot userRoot){
        RespBean respBean=RespBean.ok(null,userRootService.insert(userRoot));
        return  respBean;
    }

    @GetMapping("/getRecordList")
    public RespBean getRecordList(Integer beginPage,Integer pageSize){
        List<UserRoot> list=userRootService.getRecordList(beginPage,pageSize);
        RespBean respBean=RespBean.error("加载错误！");
        if(list!=null) {
            respBean=RespBean.ok("ok",list);
            respBean.setTotleSize(userRootService.getRecordCount());
        }
        return respBean;

    }
}
